package com.haitonghop.modules.news;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.androidquery.callback.BitmapAjaxCallback;
import com.haitonghop.R;
import com.haitonghop.modules.news.rssreader.model.FeedEntry;
import com.haitonghop.utils.GlobalData;

/**
 * Created by thanhle on 6/15/13.
 */
public class LineRssItemAdapter extends BaseAdapter {
	private ArrayList<FeedEntry> itemList;
	private LayoutInflater inflater;
	private boolean isScrolling = false;

	public LineRssItemAdapter(Activity mActivity) {
		super();
		this.inflater = (LayoutInflater) mActivity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}

	public List<FeedEntry> getItemList() {
		return itemList;
	}

	public void setItemList(List<FeedEntry> itemList) {
		this.itemList = new ArrayList<FeedEntry>(itemList);
	}

	@Override
	public int getCount() {
		return itemList.size();
	}

	@Override
	public Object getItem(int position) {
		return itemList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return 0;
	}

	public static class ViewHolder {
		public ImageView imvItemAvatar;
		public TextView tvItemName;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder;
		if (convertView == null) {
			holder = new ViewHolder();

			convertView = inflater.inflate(R.layout.news_render_detail_modules, null);
			holder.imvItemAvatar = (ImageView) convertView.findViewById(R.id.imageView);
			holder.tvItemName = (TextView) convertView.findViewById(R.id.titleView);

			convertView.setTag(holder);
		} else
			holder = (ViewHolder) convertView.getTag();

		final FeedEntry currentItem = (FeedEntry) itemList.get(position);

		GlobalData.mAQ.id(holder.imvItemAvatar).image(R.drawable.no_preview);
		if (currentItem.getImage() != null && currentItem.getImage().length() > 0) {
			if (!isScrolling || BitmapAjaxCallback.isMemoryCached(currentItem.getImage()))
				GlobalData.mAQ.id(holder.imvItemAvatar).image(currentItem.getImage(), GlobalData.opt_image);
		}

		holder.tvItemName.setText(currentItem.getTitle());
		return convertView;
	}

	public void setScrollingStatus(boolean isScrolling) {
		this.isScrolling = isScrolling;
	}
}
